/*Copyright (C) 2009 - 2011 MASSART GAUTHIER

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../headers.h"

#define TAILLE_MAX 1000

char pendu_read_char(void) {
	char caractere[2];

	while(raw_read(caractere, 0, 2, isalpha, 0) != 1);

	return toupper(caractere[0]);
}

int pendu_gagne(char lettre_trouvee[], short taille_mot) {
	short i = 0;
	short joueurGagne = 1;

	for(i = 0 ; i < taille_mot ; i++) {
		if(lettre_trouvee[i] == 0)
			joueurGagne = 0;
	}

	return joueurGagne;
}

int pendu_recherche_lettre(char lettre, char mot_mystere[], char *lettre_trouvee) {
	short i = 0;
	short bonneLettre = 0;


	for(i = 0 ; mot_mystere[i] != '\0' ; i++) {
		if(lettre == mot_mystere[i]) {
			bonneLettre = 1;
			lettre_trouvee[i] = 1;
		}
	}

	return bonneLettre;
}

int pendu_rechercher_mot(char mot_recherche[40]) {
	long i;
	char texte[40];

	if(fichier_existe("dico.ini") == true) {
		for(i = 0; ini_getkey("MOTS", i, texte, sizearray(texte), "dico.ini") > 0; i++) {
			if(!strcmp(texte, mot_recherche)) {
				return 1;
			}
		}

		return 0;
	}
	else {
		fichier_creer("dico.ini");
		return pendu_rechercher_mot(mot_recherche);
	}
}
